package com.project.controller.serveOrderController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.bean.ServeBean;
import com.project.bean.ServeOrderBean;
import com.project.bean.UserBean;
import com.project.service.employeeService.IEmployeeService;
import com.project.service.serveOrderService.IServeOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/serveOrder")
public class ServeOrderController {
    @Autowired
    private IServeOrderService serveOrderService;
    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("/add")
    public String add(ServeOrderBean serveOrder, int userId,int serveId) {
        UserBean user = new UserBean();
        ServeBean serve = new ServeBean();
        serveOrder.setUser(user);
        serveOrder.setServe(serve);
        serveOrder.getUser().setId(userId);
        serveOrder.getServe().setId(serveId);
        serveOrderService.add(serveOrder);
        return "ok";
    }

    @RequestMapping("/findById")
    public ServeOrderBean findById(int orderId) {
        return serveOrderService.findById(orderId);
    }

    @RequestMapping("/updateEmployee")
    public String updateEmployee(Integer orderId, Integer employeeId) {
        serveOrderService.updateEmployee(orderId, employeeId);
        return "ok";
    }

    @RequestMapping("/serveOrderStart")
    public String updateStart(int orderId,int employeeId) {
        serveOrderService.update(orderId, "执行中");
        employeeService.updateState(employeeId, "服务中");

        return "ok";
    }

    @RequestMapping("/serveOrderEnd")
    public String updateEnd(int orderId) {
        serveOrderService.update(orderId, "已完成");
        ServeOrderBean serveOrder = serveOrderService.findById(orderId);
        int employeeId = serveOrder.getEmployee().getId();
        employeeService.updateState(employeeId, "空闲");
        return "ok";
    }

    @RequestMapping("/findAll")
    public PageInfo<ServeOrderBean> findAll(int pageNO, int storeId) {
        return serveOrderService.findAll(pageNO, storeId);
    }

    @RequestMapping("/findByItem")
    public PageInfo<ServeOrderBean> findByItem(int pageNO, String state, LocalDate startDate, LocalDate endDate, int userId) {
        return serveOrderService.findByItem(pageNO,state, startDate, endDate, userId);
    }
}
